<?php

namespace App\Http\Middleware;

use Intervention\Image\Facades\Image;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth as qAuth;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Hash;
use Validator;
use Illuminate\Support\Facades\Input;
use Closure;

class CheckImg
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $file = $request->file('file') ;
        if(!empty($file[0])){
            $accessKey = 'REGLFp0vy53PLg-szJvTHE_NJhBKTSyQ8fTVeA-S';
            $secretKey = 'YR2jV9TCzxOPNtOtqhd4yOk33zS2iX5qYNnMcDKx';
            $bucket = 'licheng';
            $arr = array();
            for( $i=0;$i<count($file);$i++ ){
                $auth = new qAuth($accessKey, $secretKey);
                $token = $auth->uploadToken($bucket);
                $filePath = $file[$i]->getRealPath();
//                $filePath = "C:\Users\admin\PhpstormProjects\untitled\lara4\laravel\public\\".$image[$i];
                $key = time().rand(999,9999).".jpg";
                $uploadMgr = new UploadManager();
                list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
                if ($err !== null) {
                    return back()->with('error','图片上传出错，请重试') ;
                } else {
                    $arr[]='http://ohv2zs4x6.bkt.clouddn.com/'.$ret['key'];
                }
            }
            $request->attributes->add(array('imgs'=>$arr));
        }
        return $next($request);




//        $file = Input::file('face');
//        if($file->getError()!==0){
//            return back()->with('error','上传图片错误');
//        }
//        $imgurl = date('ymdhis').rand(100000,999999).'.'.$file->getClientOriginalExtension();
//        $rel =$file -> move(public_path().'/uploads/img',$imgurl);
//        $request->all()['img']='img/'.($rel->getfileName());
//        $request->attributes->add(array('img'=>'img/'.($rel->getfileName())));
//        return $next($request);
    }
}

//wherein
